package a.a010;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Test1 {

	public static Map<String, XSSFCellStyle> initStyleMap(XSSFWorkbook xwb, String sheetName) {
		
		Map<String, XSSFCellStyle> styleMap = new HashMap<String, XSSFCellStyle>();
		
		XSSFSheet sheet = xwb.getSheet(sheetName);
		
		//遍历 row
		for(int i=sheet.getFirstRowNum(); i<=sheet.getLastRowNum(); i++) {
			
			XSSFRow row = sheet.getRow(i);
			//遍历 cell
			for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
				
				XSSFCell cell = row.getCell(j);
				
				if(cell != null) {
					styleMap.put(cell.getAddress().toString(), cell.getCellStyle());
				}
				
			}
		}
		
		for (Entry<String, XSSFCellStyle> entry : styleMap.entrySet()) {
			System.out.println(entry.getKey() + " <=> " + entry.getValue());
		}
		
		return styleMap;
	}
	
	public static void main(String[] args) throws Exception {
		
		File file = new File("src/main/java/a/a010/test1.xlsx");
		System.out.println(file.getAbsolutePath());
		
		
		FileInputStream fis = new FileInputStream(file);
		XSSFWorkbook xwb = new XSSFWorkbook(fis);
		
		initStyleMap(xwb, "MY_STYLE");
		
		XSSFRow row0 = xwb.getSheet("MY_STYLE").getRow(0);
		XSSFRow row1 = xwb.getSheet("Sheet1").getRow(0);
		XSSFRow row2 = xwb.getSheet("Sheet1").createRow(1);
		
		/** ===================================================== */
		
		XSSFCellStyle cellStyle = row0.getCell(0).getCellStyle();
		row1.setRowStyle(cellStyle);
		row2.setRowStyle(cellStyle);
		
		/** ===================================================== */
		
		FileOutputStream fo = new FileOutputStream(file);
		xwb.write(fo);
		fo.close();
	}
}
